﻿using System;
using System.Linq;
using System.IO;
using System.Globalization;
using System.Threading.Tasks;
using System.Text;
using System.Collections.Generic;
using EHR18;

namespace Common
{

    public class TTSParser : DataPaser
    {
        public static TTSConditionParser conditon = null;
        static IEnumerable<System.IO.FileInfo> GetFiles(string path, string pattern = "*.*")
        {
            if (!System.IO.Directory.Exists(path))
            {
                throw new System.IO.DirectoryNotFoundException();
            }
            string[] fileNames = null;
            List<System.IO.FileInfo> files = new List<System.IO.FileInfo>();
            fileNames = System.IO.Directory.GetFiles(path, pattern, System.IO.SearchOption.AllDirectories);
            foreach (string name in fileNames)
            {
                files.Add(new System.IO.FileInfo(name));
            }
            return files;
        }
        public static bool GetTTSFileName(string Dir, DateTime dt, out string fileName)
        {
          //  var files = GetFiles(Dir, GenneralConfig.instance.CurrentModel + "*");
            var files = GetFiles(Dir, GenneralConfig.instance.CurrentModel + "*");
            var f = files.Where(t => (t.LastWriteTime > DateTime.Today&&t.LastWriteTime<DateTime.Today.AddDays(1))).OrderBy(t => t.LastWriteTime);
            if (f != null && f.Count() > 0)
            {
                fileName = f.Last().FullName;
                return true;
            }
            else
            {
                fileName = "";
                return false;
            }

        }

        public TTSParser(string FileName)
            : base(FileName)
        {
            string[] str = ReadLastNLines(1).Split('\t');
            TestTime = DateTime.ParseExact(str[0], "yyyy-MM-dd-HH-mm-ss", new CultureInfo("zh-CN"), DateTimeStyles.None);
            TestValue = new List<double>();
            for (int i = 1; i < str.Count() - 2; i++)
            {
                double d = 0;
                if (double.TryParse(str[i], out d))
                {
                    TestValue.Add(d);
                }
                else
                {
                    TestValue.Add(double.NaN);
                }
            }
          spans = new List<TestSpan>();
            for (int i = 0; i <conditon.conditions.Count; i++)
            {

                var a = conditon.conditions[i];
                var Name = a.ItemName.PadRight(5) + a.Pin;
                bool result;
                if (a.LowerLimit < TestValue[i] && a.UpperLimit > TestValue[i])
                {
                    result = true;
                    CountManager.AddPass(Name);
                }
                else
                {
                    result = false;
                    CountManager.AddFail(Name);
                }
                spans.Add(new TestSpan()
                {

                    测试项目 = Name,
                    不良率 = CountManager.getCount(Name).NgRate,
                    不良数量 = CountManager.getCount(Name).NG.ToString(),
                        测试次数 = CountManager.getCount(Name).Total.ToString(),
                    测试结果 = result ? "PASS" : "FAIL",
                    测试条件 = a.Condition,
                    测试值 = TestValue[i].ToString(),
                    单位 = a.Unit,
                    上限值 = a.UpperLimit.ToString(),
                    下限值 = a.LowerLimit.ToString()
                });
            }
            spans.Reverse();
            Result = str[str.Length-2].ToUpper().Contains("PASS") ? true : false;
        }
        public DateTime TestTime { get; set; }
        public List<double> TestValue { get; set; }
        public bool Result { get; set; }
        List<TestSpan> spans;

        public List<TestSpan> TestSpans
        {
            get { return spans; }
            set { spans = value; }
        }
    
    }
}
